<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SmartDateTime
 *
 * @author Zsoltika
 */

class SmartDateTime extends DateTime {
  static $highdays = array();
  static $workdays = array();
  static $easters = array();
  static $encoding;
  static $today;
  static $thisMonday;
  static $thisMonthStart;

  public static function init($lang) {
    self::$today = new SmartDateTime('today');
    self::$thisMonday = new SmartDateTime('Monday this week');
    self::$thisMonthStart = new SmartDateTime('first day of this month midnight');
    
    error_log('this Monday: '.self::$thisMonday->format('c') );
    error_log('this month: '.self::$thisMonthStart->format('c') );
    
    $specialdays = json_decode( file_get_contents('specialdays.json') );
    $highdays = $specialdays->highdays;
    foreach( $highdays as $date => $desc ) {
      SmartDateTime::$highdays[] = strtotime($date,0);
    }
    foreach( $highdays->{$lang} as $date => $desc ) {
      SmartDateTime::$highdays[] = strtotime($date,0);
    }
    
    $workdays = $specialdays->workdays;
    foreach( $workdays->{$lang} as $date => $desc ) {
      SmartDateTime::$workdays[] = strtotime($desc);
    }
    $locale = explode('.',setlocale(LC_TIME,null));
    self::$encoding = 'CP'.$locale[1];
  }
  function strftime_convert($str) {
    return iconv(self::$encoding,'UTF-8',strftime($str,$this->getTimestamp()));
    // A Windows képtelen a strftime függvényt UTF-8 kódolással visszaadni.
  }

  function is_high_day() {
    $year = (int)$this->format('Y');
    $easter_date = isset(self::$easters[$year])
        ? self::$easters[$year]
        : (self::$easters[$year] = easter_date($year));

    $day = clone $this;
    $day->modify('midnight');
    if ($day->getTimestamp() == $easter_date ) return 'easter'; // Easter
    $day->modify('-1 day');
    if ($day->getTimestamp() == $easter_date ) return 'easter'; // Easter
    $day->modify('-49 days');
    if ($day->getTimestamp() == $easter_date ) return 'pentecost'; // Pentecost Monday
    $day->modify('+1 day');
    if ($day->getTimestamp() == $easter_date ) return 'pentecost'; // Pentecost Sunday

    $day = clone $this;
    $day->modify('1970');
    if (in_array($day->getTimestamp(),self::$highdays)) return true;
  }
  function is_replaced_day() {
    if (in_array($this->getTimestamp(),self::$workdays)) return true;
  }
  
  function is_today() { return self::$today == $this; }
  function is_currentmonday() { return self::$thisMonday == $this; }
  function is_currentmonth() { return self::$thisMonthStart == $this; }
  
  function formatWeekday_l() {       return $this->strftime_convert('%a');  }
  function formatWeekNumber_l() {    return $this->strftime_convert('%W');  } // TODO: %W helyett %V az igazi, de Windows-on nem fog működni
  function formatYearMonth_l() {     return $this->strftime_convert('%Y. %B');  }
  
}

